package com.doyutu.seed.entity.result;

import com.doyutu.seed.entity.enums.ResultCodeEnum;
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;

/**
 * @author DoyuTu
 * @version 0.0.1
 * spring-boot-project-seed
 */
@Data
public class Result {

    private ResultCodeEnum code;
    private String message;
    @Nullable
    private Object data;

    public Result() {
    }

    private Result(ResultCodeEnum code, String message, Object obj) {
        this.code = code;
        this.message = message;
        this.data = obj;
    }

    public Result(ResultCodeEnum resultEnum, Object obj) {
        this.code = resultEnum;
        this.message = resultEnum.getMsg();
        this.data = obj;
    }

    public static ResponseEntity ok() {
        return new ResponseEntity<>(new Result(ResultCodeEnum.OK, ResultCodeEnum.OK.getMsg()), HttpStatus.OK);
    }

    public static ResponseEntity ok(Object data) {
        return new ResponseEntity<>(new Result(ResultCodeEnum.OK, data), HttpStatus.OK);
    }

    public static ResponseEntity fail() {
        return new ResponseEntity<>(new Result(ResultCodeEnum.FAIL, ResultCodeEnum.FAIL.getMsg(), null), HttpStatus.BAD_REQUEST);
    }

    public static ResponseEntity fail(String message) {
        return new ResponseEntity<>(new Result(ResultCodeEnum.FAIL, message, null), HttpStatus.BAD_REQUEST);
    }

}
